package com.xb.sourest.ui;

import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xb.sourest.R;
import com.xb.sourest.base.BaseChildLazyfragment;
import com.xb.sourest.base.BaseFragment;
import com.xb.sourest.base.BaseLazyFragment;
import com.xb.sourest.view.MessageFragmentViewPagerAdapter;
import com.xb.sourest.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 消息
 */
public  class MessageFragment extends BaseLazyFragment {


    @BindView(R.id.view_pager)
    ViewPager2 view_pager;
    @BindView(R.id.add_more_btn)
    ImageButton add_more_btn;
    @BindView(R.id.message_txt)
    TextView message_txt;
    @BindView(R.id.contacts_phone)
    TextView contacts_phone;


    private List<Fragment> fragmentList ;

    @Override
    protected void initData() {

    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_message;
    }




    private void initFragments() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new ConversationFragment());
        fragmentList.add(new ContactFragment());
        view_pager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
        view_pager.setAdapter(new FragmentStateAdapter(getChildFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        });
        view_pager.registerOnPageChangeCallback(changeCallback);
        view_pager.setUserInputEnabled(false);
    }

    //页面改变监听
    private ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            Log.e("fragment","position= "+position);
        }

    };

    @OnClick({R.id.message_txt,R.id.contacts_phone,R.id.add_more_btn})
    public  void onClick(View view){
        switch (view.getId()){
            case R.id.message_txt:
                message_txt.setTextSize(22);
                contacts_phone.setTextSize(16);
                message_txt.setTextColor(mActivity.getResources().getColor(R.color.black));
                contacts_phone.setTextColor(mActivity.getResources().getColor(R.color.gray));
                view_pager.setCurrentItem(0);
                break;
            case R.id.contacts_phone:
                message_txt.setTextSize(16);
                contacts_phone.setTextSize(22);
                message_txt.setTextColor(mActivity.getResources().getColor(R.color.gray));
                contacts_phone.setTextColor(mActivity.getResources().getColor(R.color.black));
                view_pager.setCurrentItem(1);
                break;
            case R.id.add_more_btn:
                break;
        }
    }

    @Override
    protected void loadData() {
        initFragments();
    }

    @Override
    protected void upView() {

    }

    @Override
    public void onDestroy() {
        //销毁前调用
        view_pager.unregisterOnPageChangeCallback(changeCallback);
        super.onDestroy();
    }
}
